package com.wpzf.zxhc.service.impl;

import com.aspose.words.License;
import com.wpzf.zxhc.domain.WpCjTbhcjg2023DTO;
import com.wpzf.zxhc.mapper.CommonMapper;
import com.wpzf.zxhc.service.CommonService;
import com.wpzf.zxhc.utils.NpoiHelper;
import com.wpzf.zxhc.utils.PdfUnits;
import com.wpzf.zxhc.utils.WordUnits;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    CommonMapper commonmapper;

    @Value("${tempath}")
    public String tempath;

    @Override
    public int addHcjg(Map<String, Object> map) {
        return commonmapper.addHcjg(map);
    }

    @Override
    public int addHcjg_hf_DtPwwj(Map<String, Object> map) {
        return commonmapper.addHcjg_hf_DtPwwj(map);
    }

    @Override
    public int addHcjg_wf_wfpzwh(Map<String, Object> map) {
        return commonmapper.addHcjg_wf_wfpzwh(map);
    }

    @Override
    public int addHcjg_wf_wftgzmwj(Map<String, Object> map) {
        return commonmapper.addHcjg_wf_wftgzmwj(map);
    }

    @Override
    public int adddHcjg_qt_qtpzwh(Map<String, Object> map) {
        return commonmapper.adddHcjg_qt_qtpzwh(map);
    }

    @Override
    public int addHcjg_qt_qtsftgxxydss(Map<String, Object> map) {
        return commonmapper.addHcjg_qt_qtsftgxxydss(map);
    }

    @Override
    public int addHcjg_qt_qtsfzygd(Map<String, Object> map) {
        return commonmapper.addHcjg_qt_qtsfzygd(map);
    }

    @Override
    public int deleteHcJg(Map<String, Object> map) {
        return commonmapper.deleteHcJg(map);
    }

    @Override
    public int deleteHcjg_hf_dtPwwj(Map<String, Object> map) {
        return commonmapper.deleteHcjg_hf_dtPwwj(map);
    }

    @Override
    public int deleteHcjg_wf_wfpzwh(Map<String, Object> map) {
        return commonmapper.deleteHcjg_wf_wfpzwh(map);
    }

    @Override
    public int deleteHcjg_wf_wftgzmwj(Map<String, Object> map) {
        return commonmapper.deleteHcjg_wf_wftgzmwj(map);
    }

    @Override
    public int deleteHcjg_qt_qtpzwh(Map<String, Object> map) {
        return commonmapper.deleteHcjg_qt_qtpzwh(map);
    }

    @Override
    public int deleteHcjg_qt_qtsftgxxydss(Map<String, Object> map) {
        return commonmapper.deleteHcjg_qt_qtsftgxxydss(map);
    }

    @Override
    public int deleteHcjg_qt_qtsfzygd(Map<String, Object> map) {
        return commonmapper.deleteHcjg_qt_qtsfzygd(map);
    }

    @Override
    public int updateHcjg_hcjgsm(Map<String, Object> map) {
        return commonmapper.updateHcjg_hcjgsm(map);
    }

    @Override
    public Map<String, Object> queryHcJgById(Map<String, Object> map) {
        return commonmapper.queryHcJgById(map);
    }

    @Override
    public List<Map<String, Object>> queryHcJgByColumnName() {
        return commonmapper.queryHcJgByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryHftgzmwjByColumnName() {
        return commonmapper.queryHftgzmwjByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryWfpzwhByColumnName() {
        return commonmapper.queryWfpzwhByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryWftgzmwjByColumnName() {
        return commonmapper.queryWftgzmwjByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryQtpzwhByColumnName() {
        return commonmapper.queryQtpzwhByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryQtsftgxxydssByColumnName() {
        return commonmapper.queryQtsftgxxydssByColumnName();
    }

    @Override
    public List<Map<String, Object>> queryQtsfzygdByColumnName() {
        return commonmapper.queryQtsfzygdByColumnName();
    }

    @Override
    public Map<String, Object> getHcjgsmBytbbh(Map<String, Object> parms) {
        return commonmapper.getHcjgsmBytbbh(parms);
    }

    @Override
    public List<WpCjTbhcjg2023DTO> getTbhcjglist(Map<String, Object> parm) {
        return commonmapper.getTbhcjglist(parm);
    }

    @Override
    public List<Map<String, Object>> get_user_pchlist(Map<String, Object> parms) {
        return commonmapper.get_user_pchlist(parms);
    }

    @Override
    public List<Map<String, Object>> get_unit_userlist(Map<String, Object> parm) {
        return commonmapper.get_unit_userlist(parm);
    }

    @Override
    public List<Map<String, Object>> getTbbhList(Map<String, Object> parm) {
        return commonmapper.getTbbhList(parm);
    }

    @Override
    public Map<String, Object> query_user_realname(Map<String, Object> map) {
        return commonmapper.query_user_realname(map);
    }

    @Override
    public int updateHcjg_sftj(Map<String, Object> map) {
        return commonmapper.updateHcjg_sftj(map);
    }

    @Override
    public List<Map<String, Object>> getJlPch(Map<String, Object> parm) {
        return commonmapper.getJlPch(parm);
    }

    @Override
    public Integer savePdfInfo(Map<String, Object> parm) {
        commonmapper.deletePdfInfo(parm);
        return commonmapper.savePdfInfo(parm);
    }

    @Override
    public List<Map<String, Object>> getPdfInfo(Map<String, Object> parm) {
        return commonmapper.getPdfInfo(parm);
    }

    @Override
    public List<Map<String, Object>> getHcjgsmlx(Map<String, Object> parm) {
        return commonmapper.getHcjgsmlx(parm);
    }

    @Override
    public String getPDF(Map<String, Object> parm, HttpServletResponse response) throws Exception {
        String rest = "";
        try {
//        String saveDirectory = "D:\\Tomcat8.5\\webapps\\upload";
            if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
                return "";
            }
            String yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String wordFilePath = tempath + "/WORD_" + yyyyMMddHHmmss + ".docx";
            String pdfFilePath = tempath + "/PDF_" + yyyyMMddHHmmss + ".pdf";

            String tbbh = parm.get("tbbh") + "";
            String pdlx = parm.get("pdlx") + "";

            String qsx = parm.get("qsx") + "";
            String qsxUrl = "";
            if (!"".equals(qsx)) {
                qsxUrl = downloadFile(parm.get("qsx") + "", tempath, "qsx");
            }
            String hsx = parm.get("hsx") + "";
            String hsxUrl = "";
            if (!"".equals(hsx)) {
                hsxUrl = downloadFile(parm.get("hsx") + "", tempath, "hsx");
            }
            String zp1 = parm.get("zp1") + "";
            String zp1Url = "";
            if (!"".equals(zp1)) {
                zp1Url = downloadFile(parm.get("zp1") + "", tempath, "zp1");
            }
            String zp2 = parm.get("zp2") + "";
            String zp2Url = "";
            if (!"".equals(zp2)) {
                zp2Url = downloadFile(parm.get("zp2") + "", tempath, "zp2");
            }

            if ("其他".equals(pdlx)) {
                String qtsjytjymc = parm.get("qtsjytjymc") + "";
                String nzyjsydxmtbzj = parm.get("nzyjsydxmtbzj") + "";
                String jcmj = parm.get("jcmj") + "";
                String tbdkqzmj = parm.get("tbdkqzmj") + "";
                String tbdkqzgdmj = parm.get("tbdkqzgdmj") + "";
                String tbdkqzjsydmj = parm.get("tbdkqzjsydmj") + "";
                String tbdkqznydmj = parm.get("tbdkqznydmj") + "";
                String tbdkqzwlydmj = parm.get("tbdkqzwlydmj") + "";
                String tbdkqzjbntmj = parm.get("tbdkqzjbntmj") + "";
                String lsydpzwh = parm.get("lsydpzwh") + "";
                String ssnydpzwh = parm.get("ssnydpzwh") + "";
                String cchcjg = parm.get("cchcjg") + "";
                String cchcjgsm = parm.get("cchcjgsm") + "";
                String fhhcjg = parm.get("fhhcjg") + "";
                String fhhcjgsm = parm.get("fhhcjgsm") + "";
                String tbqksm = parm.get("tbqksm") + "";

                FileInputStream fis = new FileInputStream(tempath + "\\导出PDF模板\\卫片其他模板.docx");
                XWPFDocument document = new XWPFDocument(fis);
                WordUnits.replaceText(document, "{tbbh}", tbbh);

                if (!"".equals(qsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{qsx}", qsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{qsx}", "");
                }
                if (!"".equals(hsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{hsx}", hsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{hsx}", "");
                }
                if (!"".equals(zp1)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp1}", zp1Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp1}", "");
                }
                if (!"".equals(zp2)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp2}", zp2Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp2}", "");
                }

                WordUnits.replaceTablePlaceholders(document, "{qtsjytjymc}", qtsjytjymc);
                WordUnits.replaceTablePlaceholders(document, "{nzyjsydxmtbzj}", nzyjsydxmtbzj);
                WordUnits.replaceTablePlaceholders(document, "{jcmj}", jcmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzmj}", tbdkqzmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzgdmj}", tbdkqzgdmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjsydmj}", tbdkqzjsydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqznydmj}", tbdkqznydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzwlydmj}", tbdkqzwlydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjbntmj}", tbdkqzjbntmj);
                WordUnits.replaceTablePlaceholders(document, "{lsydpzwh}", lsydpzwh);
                WordUnits.replaceTablePlaceholders(document, "{ssnydpzwh}", ssnydpzwh);
                WordUnits.replaceTablePlaceholders(document, "{cchcjg}", cchcjg);
                WordUnits.replaceTablePlaceholders(document, "{cchcjgsm}", cchcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjg}", fhhcjg);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjgsm}", fhhcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{tbqksm}", tbqksm);

                FileOutputStream fos = new FileOutputStream(wordFilePath);
                document.write(fos);
                fos.close();
                fis.close();
                document.close();

                // PDF文件流
//            ByteArrayOutputStream byteArrayOutputStream = WordUnits.wordToPdfStream(wordFilePath);
//            return byteArrayOutputStream.toString();
            } else if ("违法".equals(pdlx)) {
                String nzyjsydxmtbzj = parm.get("nzyjsydxmtbzj") + "";
                String jcmj = parm.get("jcmj") + "";
                String tbdkqzmj = parm.get("tbdkqzmj") + "";
                String tbdkqzgdmj = parm.get("tbdkqzgdmj") + "";
                String tbdkqzjsydmj = parm.get("tbdkqzjsydmj") + "";
                String tbdkqznydmj = parm.get("tbdkqznydmj") + "";
                String tbdkqzwlydmj = parm.get("tbdkqzwlydmj") + "";
                String tbdkqzjbntmj = parm.get("tbdkqzjbntmj") + "";
                String wflxyjmc = parm.get("wflxyjmc") + "";
                String zglsdwqk = parm.get("zglsdwqk") + "";
                String nzypzwh = parm.get("nzypzwh") + "";
                String xxydpzwh = parm.get("xxydpzwh") + "";
                String qtjsydpzwh = parm.get("qtjsydpzwh") + "";
                String cchcjg = parm.get("cchcjg") + "";
                String cchcjgsm = parm.get("cchcjgsm") + "";
                String fhhcjg = parm.get("fhhcjg") + "";
                String fhhcjgsm = parm.get("fhhcjgsm") + "";
                String hswtbz = parm.get("hswtbz") + "";

                FileInputStream fis = new FileInputStream(tempath + "\\导出PDF模板\\卫片违法模板.docx");
                XWPFDocument document = new XWPFDocument(fis);
                WordUnits.replaceText(document, "{tbbh}", tbbh);

                if (!"".equals(qsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{qsx}", qsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{qsx}", "");
                }
                if (!"".equals(hsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{hsx}", hsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{hsx}", "");
                }
                if (!"".equals(zp1)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp1}", zp1Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp1}", "");
                }
                if (!"".equals(zp2)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp2}", zp2Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp2}", "");
                }

                WordUnits.replaceTablePlaceholders(document, "{nzyjsydxmtbzj}", nzyjsydxmtbzj);
                WordUnits.replaceTablePlaceholders(document, "{jcmj}", jcmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzmj}", tbdkqzmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzgdmj}", tbdkqzgdmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjsydmj}", tbdkqzjsydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjsydmj}", tbdkqzjsydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqznydmj}", tbdkqznydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzwlydmj}", tbdkqzwlydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjbntmj}", tbdkqzjbntmj);
                WordUnits.replaceTablePlaceholders(document, "{wflxyjmc}", wflxyjmc);
                WordUnits.replaceTablePlaceholders(document, "{zglsdwqk}", zglsdwqk);
                WordUnits.replaceTablePlaceholders(document, "{nzypzwh}", nzypzwh);
                WordUnits.replaceTablePlaceholders(document, "{xxydpzwh}", xxydpzwh);
                WordUnits.replaceTablePlaceholders(document, "{qtjsydpzwh}", qtjsydpzwh);
                WordUnits.replaceTablePlaceholders(document, "{cchcjg}", cchcjg);
                WordUnits.replaceTablePlaceholders(document, "{cchcjgsm}", cchcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjg}", fhhcjg);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjgsm}", fhhcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{hswtbz}", hswtbz);

                FileOutputStream fos = new FileOutputStream(wordFilePath);
                document.write(fos);
                fos.close();
                fis.close();
                document.close();

                // PDF文件流
//            ByteArrayOutputStream byteArrayOutputStream = WordUnits.wordToPdfStream(wordFilePath);
//            return byteArrayOutputStream.toString();
            } else if ("合法".equals(pdlx)) {
                String nzyjsydxmtbzj = parm.get("nzyjsydxmtbzj") + "";
                String pzwh = parm.get("pzwh") + "";
                String jcmj = parm.get("jcmj") + "";
                String tbdkqzmj = parm.get("tbdkqzmj") + "";
                String tbdkqzgdmj = parm.get("tbdkqzgdmj") + "";
                String tbdkqzjsydmj = parm.get("tbdkqzjsydmj") + "";
                String tbdkqznydmj = parm.get("tbdkqznydmj") + "";
                String tbdkqzwlydmj = parm.get("tbdkqzwlydmj") + "";
                String ysxdlrdgdmj = parm.get("ysxdlrdgdmj") + "";
                String wsxdlrdgdmj = parm.get("wsxdlrdgdmj") + "";
                String tbdkqzjbntmj = parm.get("tbdkqzjbntmj") + "";
                String cchcjg = parm.get("cchcjg") + "";
                String cchcjgsm = parm.get("cchcjgsm") + "";
                String fhhcjg = parm.get("fhhcjg") + "";
                String fhhcjgsm = parm.get("fhhcjgsm") + "";
                String hswtsm = parm.get("hswtsm") + "";

                FileInputStream fis = new FileInputStream(tempath + "\\导出PDF模板\\卫片合法模板.docx");
                XWPFDocument document = new XWPFDocument(fis);
                WordUnits.replaceText(document, "{tbbh}", tbbh);

                if (!"".equals(qsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{qsx}", qsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{qsx}", "");
                }
                if (!"".equals(hsx)) {
                    WordUnits.replaceTextWithImageInTables(document, "{hsx}", hsxUrl, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{hsx}", "");
                }
                if (!"".equals(zp1)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp1}", zp1Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp1}", "");
                }
                if (!"".equals(zp2)) {
                    WordUnits.replaceTextWithImageInTables(document, "{zp2}", zp2Url, 200, 200);
                } else {
                    WordUnits.replaceTablePlaceholders(document, "{zp2}", "");
                }

                WordUnits.replaceTablePlaceholders(document, "{nzyjsydxmtbzj}", nzyjsydxmtbzj);
                WordUnits.replaceTablePlaceholders(document, "{pzwh}", pzwh);
                WordUnits.replaceTablePlaceholders(document, "{jcmj}", jcmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzmj}", tbdkqzmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzgdmj}", tbdkqzgdmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjsydmj}", tbdkqzjsydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqznydmj}", tbdkqznydmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzwlydmj}", tbdkqzwlydmj);
                WordUnits.replaceTablePlaceholders(document, "{ysxdlrdgdmj}", ysxdlrdgdmj);
                WordUnits.replaceTablePlaceholders(document, "{wsxdlrdgdmj}", wsxdlrdgdmj);
                WordUnits.replaceTablePlaceholders(document, "{tbdkqzjbntmj}", tbdkqzjbntmj);
                WordUnits.replaceTablePlaceholders(document, "{cchcjg}", cchcjg);
                WordUnits.replaceTablePlaceholders(document, "{cchcjgsm}", cchcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjg}", fhhcjg);
                WordUnits.replaceTablePlaceholders(document, "{fhhcjgsm}", fhhcjgsm);
                WordUnits.replaceTablePlaceholders(document, "{hswtsm}", hswtsm);

                FileOutputStream fos = new FileOutputStream(wordFilePath);
                document.write(fos);
                fos.close();
                fis.close();
                document.close();

                // PDF文件流
//            ByteArrayOutputStream byteArrayOutputStream = WordUnits.wordToPdfStream(wordFilePath);
//            return byteArrayOutputStream.toString();
            }
            NpoiHelper.doc2pdf(wordFilePath, pdfFilePath);
            rest = "PDF_" + yyyyMMddHHmmss + ".pdf";

            List<Map<String, Object>> attachments = (List<Map<String, Object>>) parm.get("attachments");
            if (attachments != null && attachments.size() > 0) {
                PDDocument doc = new PDDocument();
                PdfUnits.appendPDF(doc, pdfFilePath);
                for (Map<String, Object> attachment : attachments) {
                    String file = downloadFile(attachment.get("fjurl") + "", tempath);
                    String extension = getFileExtension(file);
                    switch (extension) {
                        case ".jpg":
                        case ".png":
                            PdfUnits.addImageToPDF(doc, file);
                            break;
                        case ".docx":
                        case ".doc":
                            PdfUnits.addDocxToPDF(doc, file, tempath + "/wordToPDF_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".pdf");
                            break;
                        case ".pdf":
                            PdfUnits.appendPDF(doc, file);
                            break;
                        default:
                            System.out.println("Unsupported file format: " + file);
                    }
                }
                doc.save(pdfFilePath);
            }

            File file = new File(pdfFilePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fileInputStream.read(bytes);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=\"file.pdf\"");
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = byteArrayInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            byteArrayInputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception ex) {
            rest = "error " + ex.toString();
        }
        return rest;
    }

    public static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
                    "<License>\n" +
                    "    <Data>\n" +
                    "        <Products>\n" +
                    "            <Product>Aspose.Total for Java</Product>\n" +
                    "            <Product>Aspose.Words for Java</Product>\n" +
                    "        </Products>\n" +
                    "        <EditionType>Enterprise</EditionType>\n" +
                    "        <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
                    "        <LicenseExpiry>20991231</LicenseExpiry>\n" +
                    "        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
                    "    </Data>\n" +
                    "    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
                    "</License>\n";
            is = new ByteArrayInputStream(xml.getBytes());
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    // 从给定的URL下载文件，并保存到指定目录
    private String downloadFile(String fileURL, String saveDirectory, String type) throws IOException {
        // 确保文件夹存在
        File directory = new File(saveDirectory);
        if (!directory.exists()) {
            if (directory.mkdirs()) {
                System.out.println("目录不存在，已创建: " + saveDirectory);
            } else {
                throw new IOException("无法创建目录: " + saveDirectory);
            }
        }

        // 生成基于当前时间的文件名
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSSXXX").format(new Date());
        String fileName = "/PDF_file_" + type + "_" + timestamp + getFileExtension(fileURL);
        String saveFilePath = saveDirectory + File.separator + fileName;

        // 下载文件
        URL url = new URL(fileURL);
        try (InputStream inputStream = new BufferedInputStream(url.openStream());
             FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath)) {

            byte[] dataBuffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);
            }
        }
        return saveFilePath;
    }

    private String downloadFile(String fileURL, String saveDirectory) throws IOException {
        // 确保文件夹存在
        File directory = new File(saveDirectory);
        if (!directory.exists()) {
            if (directory.mkdirs()) {
                System.out.println("目录不存在，已创建: " + saveDirectory);
            } else {
                throw new IOException("无法创建目录: " + saveDirectory);
            }
        }

        // 生成基于当前时间的文件名
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSSXXX").format(new Date());
        String fileName = "/PDF_file_" + timestamp + getFileExtension(fileURL);
        String saveFilePath = saveDirectory + fileName;

        // 下载文件
        URL url = new URL(fileURL);
        try (InputStream inputStream = new BufferedInputStream(url.openStream());
             FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath)) {

            byte[] dataBuffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);
            }
        }
        return saveFilePath;
    }

    // 获取文件的扩展名
    private static String getFileExtension(String fileURL) {
        String fileName = fileURL.substring(fileURL.lastIndexOf('/') + 1);
        if (fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf('.'));
        } else {
            return "";  // 如果没有扩展名，返回空字符串
        }
    }
}
